package io.github.yangyouwang.crud.app.controller;

import io.github.yangyouwang.common.annotation.CrudLog;
import io.github.yangyouwang.common.base.CrudController;
import io.github.yangyouwang.common.domain.Result;
import io.github.yangyouwang.common.domain.TableDataInfo;
import io.github.yangyouwang.common.enums.BusinessType;
import io.github.yangyouwang.crud.app.entity.User;
import io.github.yangyouwang.crud.app.service.DeviceService;
import io.github.yangyouwang.crud.app.service.InfoService;
import io.github.yangyouwang.crud.app.service.SiteService;
import io.github.yangyouwang.crud.app.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 */
@Api(tags = "用户表")
@Controller
@RequestMapping("/app/user")
public class UserController extends CrudController {

    private static final String SUFFIX = "app/user";

    @Autowired
    private UserService userService;

    @Autowired
    private SiteService siteService;

    @Autowired
    private InfoService infoService;
    @Autowired
    private DeviceService deviceService;

    @GetMapping("/listPage")
    public String listPage() {
        return SUFFIX + "/list";
    }

    @ApiOperation(value = "用户表分页列表", response = User.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页记录数", dataType = "Integer", paramType = "query")
    })
    @PreAuthorize("hasAuthority('user:list')")
    @GetMapping(value = "/page")
    @ResponseBody
    public TableDataInfo page(User param) {
        startPage();
        List<User> data = userService.page(param);
        return getDataTable(data);
    }

    @GetMapping("/editPage/{id}")
    public String editPage(@Valid @NotNull(message = "id不能为空") @PathVariable Long id, ModelMap map) {
        //Object data = userService.info(id);
        Object data = deviceService.info(id);
        map.put("ad", data);
        return SUFFIX + "/edit";
    }

    @ApiOperation(value = "修改")
    @PreAuthorize("hasAuthority('user:edit')")
    @PostMapping(value = "/modify")
    @ResponseBody
    @CrudLog(title = "修改信息", businessType = BusinessType.UPDATE)
    public Result modify(@RequestBody @Validated Map param, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.failure(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        deviceService.modify(param);
        return Result.success();
    }

 /*   @GetMapping("/infoPage/{id}")
    public String infoPage(@Valid @NotNull(message = "id不能为空") @PathVariable Long id, ModelMap map) {
        UserInfoVO data = userService.info(id);
        map.put("user", data);
        return SUFFIX + "/info";
    }*/

    @GetMapping("/addPage")
    public String addPage() {
        return SUFFIX + "/add2";
    }

    @GetMapping("/addPage2")
    public String addPage2() {
        return SUFFIX + "/add3";
    }
    @GetMapping("/addPage3")
    public String addPage3() {
        return SUFFIX + "/add4";
    }
    @ApiOperation(value = "用户新增")
    @PreAuthorize("hasAuthority('user:add')")
    @PostMapping(value = "/add")
    @ResponseBody
    @CrudLog(title = "新增用户", businessType = BusinessType.INSERT)
    public Result add(@RequestBody @Validated Map param, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.failure(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        siteService.add(param);
        infoService.add(param);
        return Result.success();
    }

    @ApiOperation(value = "用户新增")
    @PreAuthorize("hasAuthority('user:add')")
    @PostMapping(value = "/add2")
    @ResponseBody
    @CrudLog(title = "新增用户", businessType = BusinessType.INSERT)
    public Result add2(@RequestBody @Validated Map param, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Result.failure(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        deviceService.add(param);
        return Result.success();
    }

    @ApiOperation(value = "删除(单个条目)")
    @PreAuthorize("hasAuthority('user:del')")
    @DeleteMapping(value = "/remove/{id}")
    @ResponseBody
    @CrudLog(title = "删除信息", businessType = BusinessType.DELETE)
    public Result remove(@Valid @NotNull(message = "id不能为空") @PathVariable Long id) {
        deviceService.remove(id);
        return Result.success();
    }

}
